package com.max.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.max.constants.ReservationState;
import com.max.constants.RoomState;
import com.max.entity.MettingRoom;
import com.max.entity.RoomReservation;
import com.max.entity.TimeSlot;
import com.max.mapper.MettingRoomMapper;
import com.max.mapper.RoomReservationMapper;
import com.max.mapper.TimeSlotMapper;
import com.max.service.IMettingRoomService;
import com.max.vo.TimeSlotVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MettingRoomServiceImpl extends ServiceImpl<MettingRoomMapper, MettingRoom> implements IMettingRoomService {


    @Autowired
    private RoomReservationMapper roomReservationMapper;

    @Autowired
    private TimeSlotMapper timeSlotMapper;


    // 创建当天所有时段的List
    // 标记已经被预约的和不可用的时段
    // 返回
    @Override
    public List<TimeSlotVO> selectRoomTimeDetailById(Long roomId, String useDate) {
        // 查询预约情况
        List<RoomReservation> roomReservations = roomReservationMapper.selectList(new LambdaQueryWrapper<RoomReservation>()
                .eq(RoomReservation::getRoomId, roomId)
                .eq(RoomReservation::getUseDate, useDate)
        );
        Map<String, RoomReservation> reservationMap = roomReservations.stream().collect(Collectors.toMap(
                i -> String.format("%s_%s", i.getRoomId(), i.getTimeNum()),
                i -> i
        ));
        //查询时间段不可用设置情况
        List<TimeSlot> timeSlots = timeSlotMapper.selectList(new LambdaQueryWrapper<TimeSlot>()
                .eq(TimeSlot::getRoomId, roomId)
                .eq(TimeSlot::getUseDate, useDate));
        Map<String, TimeSlot> timeSlotMap = timeSlots.stream().collect(Collectors.toMap(
                i -> String.format("%s_%s", i.getRoomId(), i.getTimeNum()),
                i -> i
        ));
        int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        boolean today = LocalDate.now().toString().equals(useDate);

        List<TimeSlotVO> timeSlot24VoList = new ArrayList<>(24);
        for (int i = 0; i < 24; i++) {
            TimeSlotVO item = new TimeSlotVO();
            item.setUseDate(useDate);
            item.setTimeNum(i);
            item.setRoomId(roomId);
            String key = String.format("%s_%s", roomId, i);
            RoomReservation roomReservation = reservationMap.get(key);
            if ((!today || i >= nowHour) && !timeSlotMap.containsKey(key) && (roomReservation == null || ReservationState.CANCELED.isMuch(roomReservation.getReservationState()))) {
                item.setRoomState(RoomState.CAN_USE.getValue());
            } else {
                item.setRoomState(RoomState.CANT_USE.getValue());
            }
            timeSlot24VoList.add(item);
        }
        return timeSlot24VoList;
    }
}
